﻿using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;

class ServerManager
{
    private static ServerManager _instance;
    public static ServerManager Instance()
    {
        if (_instance == null)
        {
            _instance = new ServerManager();
        }
        return _instance;
    }

    Dictionary<string, SocketClient> dic = new Dictionary<string, SocketClient>();
    TcpListener listener;
    string IP = "192.168.11.17";
    int Port = 8500;

    private ServerManager()
    {
        IPAddress address = IPAddress.Parse(IP);
        listener = new TcpListener(address, Port);
        //绑定ip和port，进行侦听
        listener.Start();
        Console.WriteLine("开始侦听");

        //异步监听客户端连接
        listener.BeginAcceptTcpClient(OnAccecpt,null);
    }

    //客户端连接成功
    void OnAccecpt(IAsyncResult result)
    {
        //获取连接的客户端
        TcpClient client = listener.EndAcceptTcpClient(result);
        //封装到SocketClient里面，进行通信
        SocketClient remote = new SocketClient(client);

        //再次开启监听
        lock(listener)
        {
            listener.BeginAcceptTcpClient(OnAccecpt, null);
        }
    }


    //广播所有人消息<包含自己>
    public void BroadAll(SocketClient client, int protocal, string msg)
    {
        foreach (KeyValuePair<string, SocketClient> item in dic)
        {
            item.Value.SendMessage(protocal, msg);
        }
    }

    //广播给其他人
    public void BroadOther(SocketClient client,int protocal, string msg)
    {
        foreach (KeyValuePair<string, SocketClient> item in dic)
        {
            if (item.Key != client.GetIpPort())
            {
                item.Value.SendMessage(protocal,msg);
            }
        }
    }

    //把断开的客户端，移除掉
    public void RemoveClient(SocketClient client)
    {
        string username = client.userdata.username;

        Console.WriteLine(username + "：断开链接");
        dic.Remove(client.GetIpPort());

        //广播给所有人，玩家退出游戏
        BroadAll(client,Protocol.PlayerLogOut,LitJson.JsonMapper.ToJson(client.userdata));
    }

    //接收客户端消息，并进行分发
    public void OnMessage(SocketClient client,int protocol,string msg)
    {
        switch (protocol)
        {
            case Protocol.Login_CMD:
                BroadOther(client, protocol, msg);

                //登录后，才保存用户的信息
                //把连接的用户存储起来
                client.userdata = LitJson.JsonMapper.ToObject<User>(msg);
                dic.Add(client.GetIpPort(), client);

                dic[client.GetIpPort()].SendMessage(Protocol.SelfLogin_CMD,msg);
                break;
            case Protocol.PlayerPosition_CMD:
                BroadOther(client, protocol, msg);
                break;
            case Protocol.PlayerMove_CMD:
                BroadOther(client, protocol, msg);
                break;
            default:
                break;
        }
    }
}
